koreaCoefPlot = function(model1, model2, fileID) {
  
  # Get coefficients and SEs
  negcoef = model1[,1]
  negse = model1[,2]
  negcoef2 = model2[,1]
  negse2 = model2[,2]
  
  # Create data
  negData = data.frame(vars=names(negcoef), coef=negcoef, se=negse)
  negData$lower95 = negData$coef - 1.96*negData$se
  negData$upper95 = negData$coef + 1.96*negData$se
  negData$model = "All data"
  
  negData2 = data.frame(vars=names(negcoef2), coef=negcoef2, se=negse2)
  negData2$lower95 = negData2$coef - 1.96*negData2$se
  negData2$upper95 = negData2$coef + 1.96*negData2$se
  negData2$model = "Through 1952"
  
  
  # Remove variables
  sdata = rbind(negData, negData2)
  sdata = sdata %>% filter(!grepl("Intercept|phi", vars))
  
  # Make variable labels
  sdata$vars[which(sdata$vars=="ri30")] = "Recent imbalance"
  sdata$vars[which(sdata$vars=="ri60")] = "Recent imbalance"
  sdata$vars[which(sdata$vars=="ri90")] = "Recent imbalance"
  sdata$vars[which(sdata$vars=="rollBothLoss30l")] = "Recent casualties"
  sdata$vars[which(sdata$vars=="rollBothLoss60l")] = "Recent casualties"
  sdata$vars[which(sdata$vars=="rollBothLoss90l")] = "Recent casualties"
  sdata$vars[which(sdata$vars=="winter")] = "Winter"
  sdata$vars[which(sdata$vars=="stalin")] = "Post-Stalin"
  sdata$vars[which(sdata$vars=="eisenhower")] = "Eisenhower"
  sdata$vars[which(sdata$vars=="election")] = "1952 election"
  sdata$vars[which(sdata$vars=="prevComm")] = "Previous statement"
  sdata$vars[which(sdata$vars=="log(speechNum)")] = "Speech number"
  sdata$vars[which(sdata$vars=='I(speaker == "Communists")TRUE')] = "Communist delegation"
  sdata$vars[which(sdata$vars=='logCombatSorties')] = "Combat sorties"
  sdata$vars[which(sdata$vars=='log(wCombatL)')] = "Combat"
  sdata$vars[which(sdata$vars=='logRepLength60')] = "Report length"
  sdata$vars[which(sdata$vars=='logRepLength')] = "Report length"
  sdata$vars[which(sdata$vars=='rollUNCLoss30l')] = "UNC casualties"
  sdata$vars[which(sdata$vars=='rollUNCLoss60l')] = "UNC casualties"
  sdata$vars[which(sdata$vars=='rollUNCLoss69l')] = "UNC casualties"
  sdata$vars[which(sdata$vars=='rollPOW30l')] = "Communist POWs"
  sdata$vars[which(sdata$vars=='rollPOW60l')] = "Communist POWs"
  sdata$vars[which(sdata$vars=='rollPOW90l')] = "Communist POWs"
  sdata$vars[which(sdata$vars=='dayPos30')] = "Communist\nground losses"
  sdata$vars[which(sdata$vars=='dayNeg30')] = "UNC ground losses"
  sdata$vars[which(sdata$vars=='dayPos60')] = "Communist\nground losses"
  sdata$vars[which(sdata$vars=='dayNeg60')] = "UNC ground losses"
  sdata$vars[which(sdata$vars=='dayPos90')] = "Communist ground losses"
  sdata$vars[which(sdata$vars=='dayNeg90')] = "UNC ground losses"
  sdata$vars[which(sdata$vars=="factor(meetTypeMain)Sub-Delegation")] = "Subdelegation level"
  sdata$vars[which(sdata$vars=='factor(meetTypeMain)Staff')] = "Staff level"
  sdata$vars[which(sdata$vars=='factor(meetTypeMain)Other')] = "Other levels"
  
  sdata$vars = factor(sdata$vars, levels=c("Recent imbalance", "Recent casualties", "Recent relative gains",
                                           "UNC ground losses", "UNC casualties", 
                                           "Communist\nground losses", "Communist POWs",
                                           "Combat", "Report length", "Winter", "Combat sorties", 
                                           "Eisenhower", "Post-Stalin", "1952 election", 
                                           "Communist delegation",
                                           "Speech number",
                                           "Subdelegation level", "Staff level", "Other levels"))
  
  # Statistical significance  
  sdata$signif = ifelse(sdata$lower95 > 0 | sdata$upper95 < 0, "Yes", "No")
  
  # Generate coefficient plot
  coefPlot = ggplot(sdata, aes(coef, fct_rev(vars))) + geom_vline(xintercept=0, linetype=2) + 
    geom_pointrange(aes(xmin=lower95, xmax=upper95, shape=signif, color=model), position=position_dodge(width=-0.6)) + 
    theme_bw() + xlab("Coefficient estimate") +
    theme(legend.position="bottom", 
          legend.direction = "vertical") +
    scale_y_discrete(name="") + 
    scale_color_manual("Model", breaks=c("All data", "Through 1952"), 
                       values=c("gray10", "gray60")) +
    scale_shape_manual("Statistically\nsignificant (95%)", values=c(16, 15)) +
    guides(color = guide_legend(order=1),
           shape = guide_legend(order=2))
  
  coefPlot
  print(coefPlot)
  
  filename = paste0("./figures/coefPlotKorea", fileID, ".pdf")
  ggsave(filename, width=4.5, height=7)
}

koreaCoefPlot2 = function(model1, model2, fileID) {
  
  # Get coefficients and SEs
  negcoef = model1[,1]
  negse = model1[,2]
  negcoef2 = model2[,1]
  negse2 = model2[,2]
  
  # Create data
  negData = data.frame(vars=names(negcoef), coef=negcoef, se=negse)
  negData$lower95 = negData$coef - 1.96*negData$se
  negData$upper95 = negData$coef + 1.96*negData$se
  negData$model = "UNC"
  
  negData2 = data.frame(vars=names(negcoef2), coef=negcoef2, se=negse2)
  negData2$lower95 = negData2$coef - 1.96*negData2$se
  negData2$upper95 = negData2$coef + 1.96*negData2$se
  negData2$model = "Communists"
  
  
  # Remove variables
  sdata = rbind(negData, negData2)
  sdata = sdata %>% filter(!grepl("Intercept|phi", vars))
  
  # Make variable labels
  sdata$vars[which(sdata$vars=="ri30")] = "Recent imbalance"
  sdata$vars[which(sdata$vars=="ri60")] = "Recent imbalance"
  sdata$vars[which(sdata$vars=="ri90")] = "Recent imbalance"
  sdata$vars[which(sdata$vars=="rollBothLoss30l")] = "Recent casualties"
  sdata$vars[which(sdata$vars=="rollBothLoss60l")] = "Recent casualties"
  sdata$vars[which(sdata$vars=="rollBothLoss90l")] = "Recent casualties"
  sdata$vars[which(sdata$vars=="winter")] = "Winter"
  sdata$vars[which(sdata$vars=="stalin")] = "Post-Stalin"
  sdata$vars[which(sdata$vars=="eisenhower")] = "Eisenhower"
  sdata$vars[which(sdata$vars=="election")] = "1952 election"
  sdata$vars[which(sdata$vars=="prevComm")] = "Previous statement"
  sdata$vars[which(sdata$vars=="log(speechNum)")] = "Speech number"
  sdata$vars[which(sdata$vars=='I(speaker == "Communists")TRUE')] = "Communist delegation"
  sdata$vars[which(sdata$vars=='logCombatSorties')] = "Combat sorties"
  sdata$vars[which(sdata$vars=='log(wCombatL)')] = "Combat"
  sdata$vars[which(sdata$vars=='logRepLength60')] = "Report length"
  sdata$vars[which(sdata$vars=='logRepLength')] = "Report length"
  sdata$vars[which(sdata$vars=='rollUNCLoss30l')] = "UNC casualties"
  sdata$vars[which(sdata$vars=='rollUNCLoss60l')] = "UNC casualties"
  sdata$vars[which(sdata$vars=='rollUNCLoss69l')] = "UNC casualties"
  sdata$vars[which(sdata$vars=='rollPOW30l')] = "Communist POWs"
  sdata$vars[which(sdata$vars=='rollPOW60l')] = "Communist POWs"
  sdata$vars[which(sdata$vars=='rollPOW90l')] = "Communist POWs"
  sdata$vars[which(sdata$vars=='dayPos30')] = "Communist\nground losses"
  sdata$vars[which(sdata$vars=='dayNeg30')] = "UNC ground losses"
  sdata$vars[which(sdata$vars=='dayPos60')] = "Communist\nground losses"
  sdata$vars[which(sdata$vars=='dayNeg60')] = "UNC ground losses"
  sdata$vars[which(sdata$vars=='dayPos90')] = "Communist ground losses"
  sdata$vars[which(sdata$vars=='dayNeg90')] = "UNC ground losses"
  sdata$vars[which(sdata$vars=="factor(meetTypeMain)Sub-Delegation")] = "Subdelegation level"
  sdata$vars[which(sdata$vars=='factor(meetTypeMain)Staff')] = "Staff level"
  sdata$vars[which(sdata$vars=='factor(meetTypeMain)Other')] = "Other levels"
  
  sdata$vars = factor(sdata$vars, levels=c("Recent imbalance", "Recent casualties", "Recent relative gains",
                                           "UNC ground losses", "UNC casualties", 
                                           "Communist\nground losses", "Communist POWs",
                                           "Combat", "Report length", "Winter", "Combat sorties", 
                                           "Eisenhower", "Post-Stalin", "1952 election", 
                                           "Communist delegation",
                                           "Speech number",
                                           "Subdelegation level", "Staff level", "Other levels"))
  
  # Statistical significance  
  sdata$signif = ifelse(sdata$lower95 > 0 | sdata$upper95 < 0, "Yes", "No")
  
  # Generate coefficient plot
  coefPlot = ggplot(sdata, aes(coef, fct_rev(vars))) + geom_vline(xintercept=0, linetype=2) + 
    geom_pointrange(aes(xmin=lower95, xmax=upper95, shape=signif, color=model), position=position_dodge(width=-0.6)) + 
    theme_bw() + xlab("Coefficient estimate") +
    theme(legend.position="bottom", 
          legend.direction = "vertical") +
    scale_y_discrete(name="") + 
    scale_color_manual("Delegation", breaks=c("UNC", "Communists"), 
                       values=c("gray10", "gray60")) +
    scale_shape_manual("Statistically\nsignificant (95%)", values=c(16, 15)) +
    guides(color = guide_legend(order=1),
           shape = guide_legend(order=2))
  
  coefPlot
  print(coefPlot)
  
  filename = paste0("./figures/coefPlotKorea", fileID, ".pdf")
  
  ggsave(filename, width=4.5, height=7)
}